package com.apptest.android.ui.common;

import android.os.Bundle;
import android.text.Html;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.lifecycle.ViewModelProvider;

import com.apptest.android.R;
import com.apptest.android.core.Constants;
import com.apptest.android.core.bean.SubordinateTitleBean;
import com.apptest.android.databinding.FragmentSubordinatePageBinding;
import com.apptest.android.ui.ArticleList.ArticleListFragment;
import com.apptest.android.ui.ArticleList.ArticleListViewModel;
import com.google.android.material.tabs.TabLayout;

import java.util.List;

public abstract class SubordinatePageFragment extends Fragment {
    FragmentSubordinatePageBinding binding;
    private SparseArray<ArticleListFragment> fragmentSparseArray;
    protected SubordinatePageViewModel subordinatePageViewModel;
    protected int pageSource;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        binding = FragmentSubordinatePageBinding.inflate(inflater, container, false);
        subordinatePageViewModel = new ViewModelProvider(this).get(SubordinatePageViewModel.class);
        fragmentSparseArray = new SparseArray<>();
        initData();
        initView();

        return binding.getRoot();
    }

    private void initView(){
        subordinatePageViewModel.getSubordinateTitleList().observe(getViewLifecycleOwner(),
                list -> {
                    FragmentStatePagerAdapter adapter = new FragmentStatePagerAdapter(getChildFragmentManager()) {
                        @Override
                        public Fragment getItem(int position) {
                            ArticleListFragment articleListFragment = fragmentSparseArray.get(position);
                            if (articleListFragment == null) {
                                Bundle bundle = new Bundle();
                                bundle.putInt(Constants.ARTICLE_SOURCE, pageSource);
                                bundle.putInt(Constants.SOURCE_ID,
                                        subordinatePageViewModel.getSubordinateTitleList().getValue().get(position).getId());
                                articleListFragment = ArticleListFragment.newInstance(bundle);
                                fragmentSparseArray.put(position, articleListFragment);
                            }
                            return articleListFragment;
                        }

                        @Override
                        public int getCount() {
                            return subordinatePageViewModel.getSubordinateTitleList().getValue() == null ?
                                    0 : subordinatePageViewModel.getSubordinateTitleList().getValue().size();
                        }

                        @Override
                        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
                        }

                        @Override
                        public CharSequence getPageTitle(int position) {
                            return Html.fromHtml(subordinatePageViewModel.getSubordinateTitleList().getValue().get(position).getTitle());
                        }
                    };
                    binding.subordinateViewpager.setAdapter(adapter);
                    binding.subordinateTabLayout.setupWithViewPager(binding.subordinateViewpager);
                    binding.subordinateViewpager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(
                            binding.subordinateTabLayout));
                    binding.subordinateTabLayout.addOnTabSelectedListener(
                            new TabLayout.ViewPagerOnTabSelectedListener(binding.subordinateViewpager));
                    binding.subordinateTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                        @Override
                        public void onTabSelected(TabLayout.Tab tab) {
                            binding.subordinateViewpager.setCurrentItem(tab.getPosition(), false);
                        }

                        @Override
                        public void onTabUnselected(TabLayout.Tab tab) {
                        }

                        @Override
                        public void onTabReselected(TabLayout.Tab tab) {
                        }
                    });
                });
    }

    protected abstract void initData();

    @Override
    public void onDestroyView() {
        if(fragmentSparseArray!=null){
            fragmentSparseArray.clear();
            fragmentSparseArray=null;
        }
        super.onDestroyView();
        binding = null;
    }
}
